#ifndef TIMERWHEELSCHEDULER_H
#define TIMERWHEELSCHEDULER_H

#include <mutex>
#include <vector>
#include <thread>
#include <unordered_set>

#include "time_wheel.h"

class TimerWheelScheduler
{
public:
    explicit TimerWheelScheduler(uint32_t timer_step_ms = 50);

    uint32_t createTimerAt(int64_t when_ms, const TimerTask& handler);
    uint32_t createTimerAfter(int64_t delay_ms, const TimerTask& handler);
    uint32_t createTimerEvery(int64_t interval_ms, const TimerTask& handler);

    void cancelTimer(uint32_t timer_id);

    bool start();
    void stop();

    void appendTimeWheel(uint32_t scales, uint32_t scale_unit_ms, const std::string& name= "");

private:
    void run();

    TimeWheelPtr getGreatestTimeWheel();
    TimeWheelPtr getLeastTimeWheel();
private:
    std::mutex m_mutex;
    std::thread m_thread;

    bool m_stop;

    std::unordered_set<uint32_t> m_cancel_timer_ids;

    uint32_t m_timer_step_ms;
    std::vector<TimeWheelPtr> m_time_wheels;
};

#endif // TIMERWHEELSCHEDULER_H
